var APP = function () {
    return {
        // serverIP: '1.94.104.192:20000',
        serverIP: '36.26.80.127:20000',
       
        // serverIP: function () {
        //     return window.localStorage.getItem('serverIP');
        // },
        ajax: function (cmd, parm, successCallback, errorCallback) {
            if (api.connectionType == 'none') {
                vm.showLoading = false;
                vm.refreshLoading = false;
                vm.ifConfirm = false;
                vm.scanDisabled = false;

                if (typeof (layer) != 'undefined') {
                    layer.open({
                        content: '当前网络不可用,请连接网络!',
                        skin: 'msg',
                        time: 2
                    });
                }
                return false;
            }
            var url = 'http://' + APP.serverIP + '/Frame/SqlData?cmdname=' + cmd + '&platform=' + api.systemType + '【' + api.deviceId + '】';
            // console.log('url', JSON.stringify(url))
            if (APP.getUserInfo()) {
                if (!parm.hasOwnProperty("P_UserID")) {
                    parm.P_UserID = APP.getUserInfo().UserID;
                    parm.P_UserName = APP.getUserInfo().UserName;
                    parm.P_UserCode = APP.getUserInfo().UserCode;
                }
            }
            api.ajax({
                headers: {
                    'Version': api.appVersion,
                    'PackageName': 'com.wote.app'
                },
                url: url,
                data: {
                    values: parm
                },
                method: 'post',
                dataType: 'json'
            }, function (ret, err) {
                if (ret) {
                    if (ret.status == 200) {
                        if (successCallback) successCallback(ret.data);
                    } else {
                        if (vm) {
                            vm.showLoading = false;
                            vm.refreshLoading = false;
                            vm.ifConfirm = false;
                            vm.scanDisabled = false;
                            alert(cmd + '\n' + APP.serverIP + '\n' + '错误代码1:' + '\n' + JSON.stringify(ret));
                        }
                    }
                } else {
                    if (errorCallback) {
                        errorCallback(err);
                    } else {
                        if (vm) {
                            vm.showLoading = false;
                            vm.refreshLoading = false;
                            vm.ifConfirm = false;
                            vm.scanDisabled = false;
                            alert(cmd + '\n' + APP.serverIP + '\n' + '错误代码2:' + '\n' + JSON.stringify(err));
                        }
                    }
                }
            });
        },

        uploadFile: function (folder, fileSrc, callback) {
            var url = 'http://' + APP.serverIP + '/Frame/uploadFile?dirName=' + folder;
            api.ajax({
                headers: {
                    'Version': api.appVersion,
                    'PackageName': 'com.lls.app'
                },
                url: url,
                method: 'post',
                data: {
                    files: {
                        file: fileSrc
                    }
                }
            }, function (ret, err) {
                if (ret) {
                    if (ret.status && ret.status == 200) {
                        if (callback) callback(ret.data);
                    } else {
                        if (vm) {
                            vm.showLoading = false;
                            vm.refreshLoading = false;
                            vm.$toast('上传失败,请稍后再试');
                        }

                    }
                } else {
                    if (vm) {
                        vm.showLoading = false;
                        vm.refreshLoading = false;
                        vm.$toast('程序好像出错了,请稍后再试\n' + 'errcode:' + err.code);
                    }
                }
            });
        },


        setSafeTop: function () { //顶部安全高度
            var safeTop = document.getElementById('safeTop')
            safeTop.style.paddingTop = api.safeArea.top + 'px'
        },

        setSafeBottom: function () { //底部安全高度
            var vanTabbar = document.getElementById('van-tabbar')
            vanTabbar.style.paddingBottom = api.safeArea.bottom + 'px';
            window.localStorage.setItem('vanTabbar', vanTabbar.clientHeight)
        },

        keyBoardChange: function () {
            let height = document.documentElement.clientHeight
            window.onresize = () => { // 在页面大小发生变化时调用
                // 把获取到的高度赋值给根div
                document.getElementById('app').style.height = height + 'px'
            }
        },

        getCDAlarmLList: function () {
            APP.ajax('getCDAlarmLList', {}, function (res) {
                var audioPlayer = document.getElementById('audioPlayer');
                if (Number(res.ds[0].CC) > 0) {
                    audioPlayer.play();
                    if (typeof (layer) != 'undefined') {
                        layer.open({
                            content: res.ds1[0].JnoList,
                            skin: 'msg',
                            time: 20,
                            style: 'background-color: red;'
                        });
                    }
                } else {
                    // audioPlayer.pause()

                }
            })
        },
        getYCKBDataLiist: function () {
            APP.ajax('getYCKBData', { CJ: JSON.parse(window.localStorage.getItem('userInfo')).classID }, function (res) {
                // console.log(JSON.stringify(res));
                var audioPlayer = document.getElementById('audioPlayer');
                if (res.ds.length > 0) {
                    audioPlayer.play();
                    if (typeof (layer) != 'undefined') {
                        layer.open({
                            content: JSON.stringify(res.ds),
                            skin: 'msg',
                            time: 30,
                            style: 'background-color: orange;position: fixed; bottom: 70px;left:70px'
                        });
                    }
                }
                else {
                    // audioPlayer.pause()

                }
            })
        },

      

        /**
 * @description 深度克隆
 * @param {object} obj 需要深度克隆的对象
 * @param cache 缓存
 * @returns {*} 克隆后的对象或者原值（不是对象）
 */
        deepClone: function (obj, cache = new WeakMap()) {
            if (obj === null || typeof obj !== 'object') return obj;
            if (cache.has(obj)) return cache.get(obj);
            let clone;
            if (obj instanceof Date) {
                clone = new Date(obj.getTime());
            } else if (obj instanceof RegExp) {
                clone = new RegExp(obj);
            } else if (obj instanceof Map) {
                clone = new Map(Array.from(obj, ([key, value]) => [key, APP.deepClone(value, cache)]));
            } else if (obj instanceof Set) {
                clone = new Set(Array.from(obj, value => APP.deepClone(value, cache)));
            } else if (Array.isArray(obj)) {
                clone = obj.map(value => APP.deepClone(value, cache));
            } else if (Object.prototype.toString.call(obj) === '[object Object]') {
                clone = Object.create(Object.getPrototypeOf(obj));
                cache.set(obj, clone);
                for (const [key, value] of Object.entries(obj)) {
                    clone[key] = APP.deepClone(value, cache);
                }
            } else {
                clone = Object.assign({}, obj);
            }
            cache.set(obj, clone);
            return clone;
        },
        addTime: function () {
            var now = new Date();
            var signalrDataList = JSON.parse(window.localStorage.getItem('signalrData')) || [],

                years = now.getFullYear();
            var month = String(now.getMonth() + 1).padStart(2, '0');
            var day = String(now.getDate()).padStart(2, '0');
            var currentDate = `${years}-${month}-${day}`;

            signalrDataList.map((item) => {
                item.PlanDate = item.PlanDate ? item.PlanDate.split('T')[0] : ''
            })
            var alist = signalrDataList = signalrDataList.filter(item => item.PlanDate === currentDate)

            window.localStorage.setItem('signalrData', JSON.stringify(alist))
        },
        getUserInfo() {
            if (window.localStorage.getItem('userInfo')) {
                return JSON.parse(window.localStorage.getItem('userInfo'))
            } else {
                return null;
            }
        },

        setPage() {
            if (JSON.parse(window.localStorage.getItem('deviceModel')) == 'rk3568_s') {
                document.getElementById('box').style.zoom = 1.3
            } else if (JSON.parse(window.localStorage.getItem('deviceModel')) == 'ZC-328') {
                document.getElementById('box').style.zoom = 1.3
            }
        },

 
        getFormatbeforDate: function () { //一周前日期
            var nowTime = APP.getFormatDate(new Date()).split(' ')[0]
            let newDate = new Date(nowTime);
            newDate.setDate(newDate.getDate() - 6);
            var StartDate = newDate.toISOString().split('T')[0];
            return StartDate
            
        },

        getFormatDate: function (date) { //当前日期
            if (!date) date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            if (hour >= 0 && hour <= 9) {
                hour = "0" + hour;
            }
            if (minute >= 0 && minute <= 9) {
                minute = "0" + minute;
            }
            if (second >= 0 && second <= 9) {
                second = "0" + second;
            }
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + hour + seperator2 + minute
                + seperator2 + second;
            return currentdate;
        },


        getFormatBatch: function (date) { //获取当前批次
            if (!date) date = new Date();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = date.getFullYear() + '' + month + '' + strDate
            return currentdate;
        },
    }
}()